# How Does Improvement in Commuting Affect Employees? Evidence from a Natural Experiment
# Version: 20211014

## Yao Lu (luyao@sem.tsinghua.edu.cn),        Tsinghua University
## Xinzheng Shi (shixzh@sem.tsinghua.edu.cn), Tsinghua University
## Jagadeesh Sivadasan (jagadees@umich.edu),  University of Michigan
## Zhufeng Xu (zhufeng@zhufengxu.com),        Central University of Finance and Economics

if (!exists("AllScriptFlag", 1)) {
  source("./Scripts/1Header.R")
}

#### Figure 2: Trends in Differentials for Log (BonusT)
Main         <- read_dta("Data/Main.dta")
Main$quarter <- NA
## 2013
Main$quarter <- replace(Main$quarter, Main$Date == "2013-01" | Main$Date == "2013-02" | Main$Date == "2013-03", "2013Q1") 
Main$quarter <- replace(Main$quarter, Main$Date == "2013-04" | Main$Date == "2013-05" | Main$Date == "2013-06", "2013Q2") 
Main$quarter <- replace(Main$quarter, Main$Date == "2013-07" | Main$Date == "2013-08" | Main$Date == "2013-09", "2013Q3") 
Main$quarter <- replace(Main$quarter, Main$Date == "2013-10" | Main$Date == "2013-11" | Main$Date == "2013-12", "2013Q4") 
## 2014
Main$quarter <- replace(Main$quarter, Main$Date == "2014-01" | Main$Date == "2014-02" | Main$Date == "2014-03", "2014Q1") 
Main$quarter <- replace(Main$quarter, Main$Date == "2014-04" | Main$Date == "2014-05" | Main$Date == "2014-06", "2014Q2") 
Main$quarter <- replace(Main$quarter, Main$Date == "2014-07" | Main$Date == "2014-08" | Main$Date == "2014-09", "2014Q3") 
Main$quarter <- replace(Main$quarter, Main$Date == "2014-10" | Main$Date == "2014-11" | Main$Date == "2014-12", "2014Q4") 
## 2015
Main$quarter <- replace(Main$quarter, Main$Date == "2015-01" | Main$Date == "2015-02" | Main$Date == "2015-03", "2015Q1") 
Main$quarter <- replace(Main$quarter, Main$Date == "2015-04" | Main$Date == "2015-05" | Main$Date == "2015-06", "2015Q2") 
Main$quarter <- replace(Main$quarter, Main$Date == "2015-07" | Main$Date == "2015-08" | Main$Date == "2015-09", "2015Q3") 
Main$quarter <- replace(Main$quarter, Main$Date == "2015-10" | Main$Date == "2015-11" | Main$Date == "2015-12", "2015Q4") 
## 2016
Main$quarter <- replace(Main$quarter, Main$Date == "2016-01" | Main$Date == "2016-02" | Main$Date == "2016-03", "2016Q1") 
Main$quarter <- replace(Main$quarter, Main$Date == "2016-04" | Main$Date == "2016-05" | Main$Date == "2016-06", "2016Q2") 
Main$quarter <- replace(Main$quarter, Main$Date == "2016-07" | Main$Date == "2016-08" | Main$Date == "2016-09", "2016Q3") 
Main$quarter <- replace(Main$quarter, Main$Date == "2016-10" | Main$Date == "2016-11" | Main$Date == "2016-12", "2016Q4") 
Main$quarter <- as.factor(Main$quarter)
Main$quarter <- relevel(Main$quarter, ref = "2014Q4") 

Main$NearSubway_quarter <- NA
## 2013
Main$NearSubway_quarter <- replace(Main$NearSubway_quarter, Main$NearSubway == 1 & Main$quarter == "2013Q1", "2013Q1")
Main$NearSubway_quarter <- replace(Main$NearSubway_quarter, Main$NearSubway == 1 & Main$quarter == "2013Q2", "2013Q2")
Main$NearSubway_quarter <- replace(Main$NearSubway_quarter, Main$NearSubway == 1 & Main$quarter == "2013Q3", "2013Q3")
Main$NearSubway_quarter <- replace(Main$NearSubway_quarter, Main$NearSubway == 1 & Main$quarter == "2013Q4", "2013Q4")
## 2014
Main$NearSubway_quarter <- replace(Main$NearSubway_quarter, Main$NearSubway == 1 & Main$quarter == "2014Q1", "2014Q1")
Main$NearSubway_quarter <- replace(Main$NearSubway_quarter, Main$NearSubway == 1 & Main$quarter == "2014Q2", "2014Q2")
Main$NearSubway_quarter <- replace(Main$NearSubway_quarter, Main$NearSubway == 1 & Main$quarter == "2014Q3", "2014Q3")
Main$NearSubway_quarter <- replace(Main$NearSubway_quarter, Main$NearSubway == 1 & Main$quarter == "2014Q4", "2014Q4")
## 2015
Main$NearSubway_quarter <- replace(Main$NearSubway_quarter, Main$NearSubway == 1 & Main$quarter == "2015Q1", "2015Q1")
Main$NearSubway_quarter <- replace(Main$NearSubway_quarter, Main$NearSubway == 1 & Main$quarter == "2015Q2", "2015Q2")
Main$NearSubway_quarter <- replace(Main$NearSubway_quarter, Main$NearSubway == 1 & Main$quarter == "2015Q3", "2015Q3")
Main$NearSubway_quarter <- replace(Main$NearSubway_quarter, Main$NearSubway == 1 & Main$quarter == "2015Q4", "2015Q4")
## 2016
Main$NearSubway_quarter <- replace(Main$NearSubway_quarter, Main$NearSubway == 1 & Main$quarter == "2016Q1", "2016Q1")
Main$NearSubway_quarter <- replace(Main$NearSubway_quarter, Main$NearSubway == 1 & Main$quarter == "2016Q2", "2016Q2")
Main$NearSubway_quarter <- replace(Main$NearSubway_quarter, Main$NearSubway == 1 & Main$quarter == "2016Q3", "2016Q3")
Main$NearSubway_quarter <- replace(Main$NearSubway_quarter, Main$NearSubway == 1 & Main$quarter == "2016Q4", "2016Q4")
Main$NearSubway_quarter <- replace(Main$NearSubway_quarter, Main$NearSubway == 0, "untreat")
Main$NearSubway_quarter <- as.factor(Main$NearSubway_quarter)
Main$NearSubway_quarter <- relevel(Main$NearSubway_quarter, ref = "2014Q4") 


summary(fit <-  felm(LogBonusT ~ NearSubway_quarter | ID + as.factor(Company):as.factor(Date) | 0 | ID + Date,  data = Main,  exactDOF = TRUE))
grapg_trend_quarter_add_zero(fe_data = fit, level = 0.95)
ggsave("Output/Figure2.pdf", width = 5.5, height = 4.25, unit = "in", device = cairo_pdf)
ggsave("Output/Figure2.png", width = 5.5, height = 4.25, unit = "in")

rm(Main, fit)

